package com.pocketsweet.ui;

import android.os.Bundle;
import android.widget.EditText;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SaveCallback;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.LoadingDailog;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;
import com.pocketsweet.ui.uilib.HeaderLayout.onRightContainerListener;
import com.pocketsweet.utils.ToolKits;

@ContentView(R.layout.activity_edit_job)
public class EditJob extends BaseActivity {

	private HeaderLayout header;
	private MLUser mlUser = new MLUser();
	private String job;

	@ViewInject(R.id.edtJob)
	private static EditText edtJob;
	private LoadingDailog mLoadingDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		mlUser = UserService.getCurrentUser();
		initView();
		setHeaderFunction();
		mLoadingDialog = ToolKits.createLoadingDialog(EditJob.this, "登陆中...");
		mLoadingDialog.setText("加载中");
	}

	private void initView() {
		job = getIntent().getStringExtra("job");
		edtJob.setText(job);
		edtJob.setSelection(job.length());

	}

	public void setHeaderFunction() {
		header = (HeaderLayout) findViewById(R.id.mainHeader);
		header.init(HeaderStyle.IMG_TITLE_TEXT);
		header.setRightText("保存");
		header.setMiddleText("职业");
		header.setRighttContainerClickListener(new onRightContainerListener() {
			@Override
			public void onClick() {
				final String job = edtJob.getText().toString();
				if (job.length() > 32) {
					ToolKits.toast(EditJob.this, "昵称不能超过32个字");
					return;
				}
				if (!job.equals(job)) {
//					SelfFragment.needUpdateBg = true;
					ProfileEdit.needUpdate = true;
				}
				mLoadingDialog.show();
				mlUser.setJob(job);
				mlUser.saveInBackground(new SaveCallback() {
					@Override
					public void done(AVException arg0) {
						// TODO Auto-generated method stub
						mLoadingDialog.dismiss();
						if (arg0 == null) {
							
							// notify
							ProfileEdit.notifyBack();
							EditJob.this.finish();
							ToolKits.toast(EditJob.this, "修改成功");
						} else {
//							Log.e("==error", "修改昵称失败" + arg0.getMessage());
						}
					}
				});
			}
		});
		header.setLeftContainerClickListener(new onLeftContainerListener() {

			@Override
			public void onClick() {
				EditJob.this.finish();
			}
		});
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}
}
